/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller.topic;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.tieba.base.BaseController;
import com.fujitsu.nanjing.tieba.domain.ReplyInfo;
import com.fujitsu.nanjing.tieba.domain.TopicInfo;
import com.fujitsu.nanjing.tieba.exception.Resource404Exception;
import com.fujitsu.nanjing.tieba.ibator.CoreTopic;
import com.fujitsu.nanjing.tieba.service.TopicInfoService;
import com.fujitsu.nanjing.tieba.service.TopicService;

/**
 * TopicController.java
 * 
 * @author Qixiaobo-win8
 */
@Controller
@RequestMapping("/topics/")
public class TopicController extends BaseController {
	@Resource
	private TopicInfoService topicInfoService;
	@Resource
	private TopicService topicService;
	@RequestMapping(value = "/{topicid}", method = RequestMethod.GET)
	public String show(Model model, @PathVariable String topicid) {
		// 在用户访问第一页时，增加访问量
		topicService.addVisits(decrypt(topicid));
		return redirect("/topics/" + topicid + "/pages/1");
	}
	@RequestMapping(value = "/{topicid}/pages/{pageNum}", method = RequestMethod.GET)
	public String showWithPage(Model model, @PathVariable String topicid,
			@PathVariable String pageNum) throws Resource404Exception {
		topicid = decrypt(topicid);
		TopicInfo topicInfo = topicInfoService.findByIdWithPageNum(topicid,
				Integer.parseInt(pageNum));
		if (topicInfo == null) {
			throw new Resource404Exception("404,你懂得!");
		}
		pageNum = (pageNum == null ? "0" : pageNum);
		model.addAttribute("pageIndex", pageNum);
		model.addAttribute("pageCount", topicService.findPageCount(topicid));
		model.addAttribute("topicInfo", topicInfo);
		return "topic/topic";
	}
	@RequestMapping(value = "/{topicid}", method = RequestMethod.DELETE)
	public String delete(Model model, @PathVariable String topicid)
			throws Resource404Exception {
		topicid = decrypt(topicid);
		CoreTopic topic = topicService.findById(topicid);
		if (topic == null) {
			throw new Resource404Exception("404,你懂得!");
		}
		int boardId = topic.getBoardid();
		topicService.delete(topic);
		return redirect("/forum/boards/" + encrypt(String.valueOf(boardId)));
	}
	@RequestMapping(value = "/{topicid}/999", method = RequestMethod.GET)
	public String newestReply(Model model, @PathVariable String topicid)
			throws Resource404Exception {
		topicid = decrypt(topicid);
		int pagesCount = topicService.findPageCount(topicid);
		TopicInfo topicInfo = topicInfoService.findByIdWithPageNum(topicid,
				pagesCount);
		if (topicInfo == null) {
			throw new Resource404Exception("404,你懂得!");
		}
		List<ReplyInfo> infos = topicInfo.getReplyInfos();
		if (infos.size() == 0) {
			return redirect("/topics/" + encrypt(topicid) + "/pages/"
					+ pagesCount);
		}
		return redirect("/topics/" + encrypt(topicid) + "/pages/" + pagesCount
				+ "#rid" + infos.get(infos.size() - 1).getReplyid());
	}
	@RequestMapping(value = "/{topicid}/{replyid}", method = RequestMethod.GET)
	public String showReply(Model model, @PathVariable String topicid,
			@PathVariable String replyid) throws Resource404Exception {
		topicid = decrypt(topicid);
		replyid = decrypt(replyid);
		int pagesCount = topicService.findPageCount(topicid);
		TopicInfo topicInfo = topicInfoService.findByIdWithPageNum(topicid,
				pagesCount);
		if (topicInfo == null) {
			throw new Resource404Exception("404,你懂得!");
		}
		int pageIndex = topicInfoService.findPageIndexWithReplyId(topicid,
				replyid);
		if (pageIndex == -1) {
			throw new Resource404Exception("该回复不存在或已被删除！");
		}
		return redirect("/topics/" + encrypt(topicid) + "/pages/" + pageIndex
				+ "#rid" + replyid);
	}
}
